#ifndef TASKVARIABLE_H
#define TASKVARIABLE_H



#include <core/noncopyable.h>

#include <functional>

#include <QHash>
#include <QString>
#include <QSharedPointer>
#include <QScopedPointer>
#include <QProcessEnvironment>

namespace stitch
{
class Task;
class Notebook;
class Buffer;
class ViewWindow;
class TaskMgr;

class TaskVariable
{
public:
    typedef std::function<QString(Task *, const QString &)> Func;

    TaskVariable(const QString &p_name, const Func &p_func);

    QString evaluate(Task *p_task, const QString &p_value) const;

private:
    QString m_name;

    Func m_func;
};


class TaskVariableMgr : private Noncopyable
{
public:
    explicit TaskVariableMgr(TaskMgr *p_taskMgr);

    void init();

    QString evaluate(Task *p_task, const QString &p_text) const;

    QStringList evaluate(Task *p_task, const QStringList &p_texts) const;

private:
    const TaskVariable *findVariable(const QString &p_name) const;
    TaskMgr *m_taskMgr = nullptr;

    QHash<QString, TaskVariable> m_variables;

    bool m_needUpdateSystemEnvironment = true;

    QProcessEnvironment m_systemEnv;

    // %{name[:value]}%.
    // Captured texts:
    // 1 - The name of the variable (trim needed).
    // 2 - The value option of the variable if available (trim needed).
    static const QString c_variableSymbolRegExp;
};
} //

#endif // TASKVARIABLE_H
